/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.quickstart.xutils.common.util;

import java.util.Iterator;

/**
 * TextUtils
 *
 * @since 2021-05-08
 */
public class TextUtils {
    private TextUtils() {
    }

    /**
     * isEmpty
     *
     * @param str
     * @return boolean
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * join
     *
     * @param delimiter
     * @param tokens
     * @return String
     */
    public static String join(CharSequence delimiter, Object[] tokens) {
        final int length = tokens.length;
        if (length == 0) {
            return "";
        }
        final StringBuilder sb = new StringBuilder();
        sb.append(tokens[0]);
        for (int number = 1; number < length; number++) {
            sb.append(delimiter);
            sb.append(tokens[number]);
        }
        return sb.toString();
    }

    /**
     * join
     *
     * @param delimiter
     * @param tokens
     * @return String
     */
    public static String join(CharSequence delimiter, Iterable tokens) {
        final Iterator<?> it = tokens.iterator();
        if (!it.hasNext()) {
            return "";
        }
        final StringBuilder sb = new StringBuilder();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
        return sb.toString();
    }

    /**
     * isDigitsOnly
     *
     * @param str
     * @return boolean
     */
    public static boolean isDigitsOnly(CharSequence str) {
        final int len = str.length();
        for (int cp, number = 0; number < len; number += Character.charCount(cp)) {
            cp = Character.codePointAt(str, number);
            if (!Character.isDigit(cp)) {
                return false;
            }
        }
        return true;
    }
}
